import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getLowCarbonMaterialList } from '@/apis/system/config/lowCarbonMaterial'
import type { CTableProps } from '@/components/CTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'

export const useLowCarbonList = () => {
  // 搜索字段及其他
  const searchForm = reactive({
    materialName: ''
  })
  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '材料名称',
      field: 'materialName',
      type: 'input'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData,
    selection: false,
    border: false
  }

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getLowCarbonMaterialList(params)
  }
  // 处理参数
  const handleParams = () => {
    let params = {
      materialName: searchForm.materialName
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '材料名称',
      field: 'materialName',
      minWidth: 150
    },
    {
      title: '因子名称',
      field: 'factorName',
      minWidth: 150
    },
    {
      title: '因子值',
      field: 'emissionFactor',
      slot: 'emissionFactorSlot',
      minWidth: 150
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 140,
      fixed: 'right'
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
